package com.abc.robot01.vivemodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.activity.toJSONString
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Data
import com.abc.robot01.retrofit.bean.FetchDataRequest
import kotlinx.coroutines.launch

class HomePageProductViewmodel : ViewModel() {

    val TAG="HomePageProductViewmodel"
    val myLiveData: MutableLiveData<Data?> = MutableLiveData(null)
    val configs: LiveData<Data?> = myLiveData

    fun fetchData(robotId:String , homePageConfigId:String) {
        Log.e(TAG,"fetchData:"+robotId+ "homePageConfigId:"+homePageConfigId)
        viewModelScope.launch {
            try {
                myLiveData.value  = RetrofitService.instance.getHomePageCommodity(Robot.accessToken,FetchDataRequest(0,1000,robotId,homePageConfigId)).data
               Log.e(TAG,"getHomePageCommodity:" + myLiveData.value!!.toJSONString())
            } catch (e: Exception) {
                Log.e(TAG,""+e.message)
                // 处理异常
                e.printStackTrace()
            }
        }
    }
}

